<?php

class Admin_NewscatController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;        
        $this->view->headScript()->appendFile(BASE_URL.'static/assets/js/main/newscat.js');
    }
        
    public function indexAction(){
        $modelNewscat            = new Admin_Model_Newscat();
        $newsCatList             = $modelNewscat->getAllData()->toArray();        
        $this->view->newsCatList = $newsCatList;        
    }
    
    public function addAction(){    
        $form = new Admin_Form_Newscat();        
        $this->view->form = $form;
        if($this->getRequest()->isPost()){            
            $formData = $this->getRequest()->getPost();                 
            if ($form->isValid($formData)){
                $addData['name']        = $form->getValue('name');
                $addData['display']     = $form->getValue('display');
                $addData['ord']         = $form->getValue('ord');
                $addData['keywords']    = $form->getValue('keywords');
                $addData['description'] = $form->getValue('description');                                
                $addData['seourl']      = $form->getValue('seourl');                
                $addData['parent']      = $form->getValue('parent');
                
                $modelNewsCat = new Admin_Model_Newscat();
                $newsCatCheck = $modelNewsCat->getDataByUrl($addData['seourl']);
                                
                if(!$newsCatCheck){                    
                    $newsCatId = $modelNewsCat->addData($addData);                    
                    if($newsCatId){
                        $this->_helper->redirector('index', 'newscat', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                   }
                }else{
                    $this->view->mess = "Tên hoặc Mã danh mục đã tồn tại. Vui lòng chọn tên khác!";
                    $this->view->flag = "error"; 
                }
            }
        }
    }
    public function editAction(){
        $newsCatId = (int)($this->_getParam('newsCatId', 0));
        $modelNewsCat = new Admin_Model_Newscat();
        $newsCatCheck = $modelNewsCat->getDataByCatId($newsCatId);
        $form = new Admin_Form_Newscat();
        $this->view->form = $form;
        if($newsCatCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){
                    
                    $editData['id']             = $form->getValue('id');
                    $editData['name']           = $form->getValue('name');
                    $editData['keywords']       = $form->getValue('keywords');
                    $editData['description']    = $form->getValue('description');
                    $editData['display']        = $form->getValue('display');
                    $editData['ord']            = $form->getValue('ord');
                    $editData['seourl']         = $form->getValue('seourl');                
                    $editData['parent']         = $form->getValue('parent');
                        
                    $modelNewsCat = new Admin_Model_Newscat();
                    $newsCatCheck = $modelNewsCat->getDataByUrl($editData['seourl']);
                    $newsCatCheckParent = $modelNewsCat->getData(array('parent'=>$editData['parent']));                                                                 
                    if(!$newsCatCheck || $editData['id'] == $newsCatCheck['id'] || $newsCatCheckParent[0]['id'] == $editData['id']){
                        $modelNewsCat->updateData($editData['id'],$editData);
                        $this->_helper->redirector('index', 'newscat', 'admin');
                    }else{
                        $this->view->mess = "Check again!! ";
                        $this->view->messTitle = "";
                        $this->view->flag = "warning";
                    }
                }
            }else{                
                $form->populate($newsCatCheck->toArray());
            }
        }else{
            $this->_helper->redirector('index', 'newscat', 'admin');
        }
    }    
    public function deleteAction(){        
        if($this->getRequest()->isPost()){            
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){               
                $newsCatId = $this->getRequest()->getPost('newsCatId');
                $modelNewsCat = new Admin_Model_Newscat();
                $modelNewsCat->deleteData($newsCatId);
                // ghi log
            }            
            $this->_helper->redirector('index', 'newscat', 'admin');
        }else{              
            $id = $this->_getParam('newsCatId', 0);                       
            $modelNewsCat = new Admin_Model_Newscat();
            $newsCatCheck = $modelNewsCat->getDataByCatId($id);             
            if($newsCatCheck){
                $this->view->newsCatCheck = $newsCatCheck;                
            }else{                
                $this->_helper->redirector('index', 'newscat', 'admin');
            }                
        }
    }
    
    public function displayAction(){        
        $this->_helper->layout->disableLayout();        
        if($this->getRequest()->isPost()){
            if(isset($this->_params['act']) && $this->_params['act'] == 'display' && isset($this->_params['newsCatId'])){            
                $modelNewsCat = new Admin_Model_Newscat();
                $newsCatCheck = $modelNewsCat->getDataByCatId($this->_params['newsCatId']);
                if($newsCatCheck){
                    if($newsCatCheck['display'] == 1)
                        $modelNewsCat->updateData($this->_params['newsCatId'],array('display'=>0));
                    else{
                        $modelNewsCat->updateData($this->_params['newsCatId'],array('display'=>1));
                    }                
                }else{
                    return false;    
                }
            }                
        }else{
            $this->_helper->redirector('index', 'newscat', 'admin');
        }
    }
}